package com.mjh.zhujianju.service.base.impl;

import com.github.pagehelper.PageHelper;
import com.livew.framework.base.model.FileEO;
import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.FileService;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.page.PagedBeanUtil;
import com.livew.framework.page.PagedResult;
import com.livew.framework.utils.DeleteType;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.dao.base.PraiseMapper;
import com.mjh.zhujianju.enums.BusinessEnum;
import com.mjh.zhujianju.model.base.PraiseEO;
import com.mjh.zhujianju.service.base.PraiseService;
import com.mjh.zhujianju.vo.base.PraiseVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author liuyang
 * @Description: 评论
 * @date 2018/8/11 10:13
 */
@Service
public class PraiseServiceImpl extends BaseServiceImpl<PraiseEO> implements PraiseService {

    @Autowired
    private PraiseMapper praiseMapper;

    @Autowired
    private FileService fileService;

    @Autowired
    private SysUserService sysUserService;

    public PraiseServiceImpl(PraiseMapper praiseMapper) {
        super(praiseMapper);
    }

    @Override
    public Integer qeruyPraiseTotals(String type, Long businessId) {
        PraiseEO praiseEO = new PraiseEO();
        praiseEO.setType(type);
        praiseEO.setBusinessId(businessId);
        return praiseMapper.queryPraiseTotal(praiseEO);
    }

    @Override
    public String savePraiseEO(PraiseEO praiseEO) {
        boolean b1 = praiseEO.getType().equals(BusinessEnum.TYPE_WORKDYNAMIC.getCode());
        boolean c1 = StringUtils.isEmpty(praiseEO.getUserId());
        boolean d1 = StringUtils.isEmpty(praiseEO.getBusinessId());

        if (!b1 || c1 || d1){
            return "parameter";
        }

        List<PraiseEO> list = praiseMapper.queryPraiseIsAdd(praiseEO);
        if (!CollectionUtils.isEmpty(list)){
            return "repeat";
        }
        praiseEO.setCreateTime(new Date());
        praiseMapper.insertSelective(praiseEO);
        return "success";
    }

    @Override
    public String revokePraiseEO(PraiseEO praiseEO) {
        boolean b1 = praiseEO.getType().equals(BusinessEnum.TYPE_WORKDYNAMIC.getCode());
        boolean d1 = StringUtils.isEmpty(praiseEO.getBusinessId());
        if (!b1 || d1){
            return "parameter";
        }

        List<PraiseEO> list = praiseMapper.queryPraiseIsAdd(praiseEO);
        if (CollectionUtils.isEmpty(list)){
            return "error";
        }
        praiseEO = list.get(0);
        praiseEO.setIsDel(DeleteType.IS_DELETE);
        praiseEO.setUpdateTime(new Date());
        praiseMapper.updateByPrimaryKey(praiseEO);
        return "success";
    }

    @Override
    public PagedResult<PraiseVO> getPageList(PraiseVO praiseVO) {
        Integer pageNo = praiseVO.getPageNo() == null ? 1 : praiseVO.getPageNo();
        Integer pageSize = praiseVO.getPageSize() == null ? 15 : praiseVO.getPageSize();
        PageHelper.startPage(pageNo, pageSize);
        //1-查询所有评论
        PraiseEO praiseEO = new PraiseEO();
        BeanUtils.copyProperties(praiseVO,praiseEO);
        praiseEO.setType(BusinessEnum.TYPE_WORKDYNAMIC.getCode());
        List<PraiseEO> list = praiseMapper.selectPagination(praiseEO);
        PagedResult<PraiseEO> page = PagedBeanUtil.toPagedResult(list);
        PagedResult<PraiseVO> result = new PagedResult<PraiseVO>();
        BeanUtils.copyProperties(page, result);
        if (!XTListUtils.isEmpty(list)) {
            //2-获取用户id
            List<Long> userIds = list.stream().map(commentVO1 -> commentVO1.getUserId()).distinct().collect(Collectors.toList());

            List<PraiseVO> voList = new ArrayList<PraiseVO>();
            Map<Long, FileEO> files = fileService.getMapTopOne(userIds, SysUser.class.getName(), null);
            Map<Long,SysUser> sysUserMap = sysUserService.getMapTopOne(userIds);
            for (PraiseEO praiseEO1 : list) {
                PraiseVO p = new PraiseVO();
                BeanUtils.copyProperties(praiseEO1, p);
                voList.add(p);
                FileEO file = files.get(p.getUserId());
                if (file != null) {
                    p.setUserFileId(file.getId());
                }

                SysUser sysUser = sysUserMap.get(p.getUserId());
                if(sysUser != null){
                    p.setUserName(sysUser.getNickname());
                }
            }
            result.setDataList(voList);
        }
        return result;
    }


}
